package com.rupertjones.globalcron.web;

import com.rupertjones.globalcron.web.munin.MuninGraphConfig;
import com.rupertjones.globalcron.web.munin.MuninReport;
import com.rupertjones.globalcron.web.munin.MuninResults;
import com.rupertjones.globalcron.web.service.ExecutionsMuninReport;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;

/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
public class InfoStatisticsAction extends AbstractAction {

    private String action = null;

    private String output = null;

    private String report = null;

    @SpringBean
    private ExecutionsMuninReport executionsReport;

    @Before(stages = LifecycleStage.BindingAndValidation)
    public void loadAndBind() {
        action = getContext().getRequest().getParameter("r");
        report = getContext().getRequest().getParameter("t");
    }

    @DefaultHandler
    public Resolution view() {
        MuninReport muninReport = null;
        if("executions".equals(report)) {
            muninReport = executionsReport;
        } else {
            muninReport = new NothingReport();
        }

        if("config".equals(action)) {
            output = muninReport.getConfig().output();
        } else {
            output = muninReport.getResults().output();
        }

        return new ForwardResolution("/WEB-INF/jsp/statistics.jsp");
    }

    public String getOutput() {
        return output;
    }

    private class NothingReport implements MuninReport {

        @Override
        public MuninGraphConfig getConfig() {
            return new MuninGraphConfig();
        }

        @Override
        public MuninResults getResults() {
            return new MuninResults();
        }
    }
}
